From 3f48beabb58be75147197ee55f37673808d4c176 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Elio=20Petten=C3=B2?= Date: Sun, 29 Sep 2013 22:17:22 +0100 Subject: Read more data out of the OTUltra2 dump lines. Return data in a Reading object. The Reading object will be extended to cater for flags, comments and other device-specific information. --- glucometer.py | 4 ++-- glucometerutils/common.py | 19 +++++++++++++++++++ glucometerutils/drivers/otultra2.py | 21 +++++++++++---------- 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/glucometer.py b/glucometer.py index c2504f2..facd88b 100755 --- a/glucometer.py +++ b/glucometer.py @@ -45,8 +45,8 @@ def main(): device = driver.Device(args.device) if args.action == 'dump': - for reading in device.get_readings(args.unit): - print('%s,%f' % reading) + for reading in device.get_readings(): + print('%s,%f' % (reading.timestamp, reading.get_value_as(args.unit))) elif args.action == 'datetime': if args.set == 'now': print(device.set_datetime()) diff --git a/glucometerutils/common.py b/glucometerutils/common.py index 1f8d59d..a93ecf0 100644 --- a/glucometerutils/common.py +++ b/glucometerutils/common.py @@ -47,3 +47,22 @@ def convert_glucose_unit(value, from_unit, to_unit=None): return round(value / 18.0, 2) else: return round(value * 18.0, 0) + + +class Reading(object): + def __init__(self, timestamp, value, unit): + self.timestamp = timestamp + self.value = value + self.unit = unit + + def get_value_as(self, to_unit): + """Returns the reading value as the given unit. + + Args: + to_unit: either UNIT_MGDL or UNIT_MMOLL as wanted; if None, the + value as recorded will be returned. + """ + if to_unit is None: + return self.value + + return convert_glucose_unit(self.value, self.unit, to_unit) diff --git a/glucometerutils/drivers/otultra2.py b/glucometerutils/drivers/otultra2.py index 9770b66..4eb2eb1 100644 --- a/glucometerutils/drivers/otultra2.py +++ b/glucometerutils/drivers/otultra2.py @@ -210,10 +210,13 @@ class Device(object): match = self._GLUCOSE_UNIT_RE.match(response) return self._parse_glucose_unit(match.group(1)) - _DUMP_HEADER_RE = re.compile(r'P ([0-9]{3}),"[0-9A-Z]{9}","(MG/DL |MMOL/L)"') - _DUMP_LINE_RE = re.compile(r'P ("[A-Z]{3}","[0-9/]{8}","[0-9:]{8} "),"([ 0-9.]{6})",') + _DUMP_HEADER_RE = re.compile(r'P ([0-9]{3}),"[0-9A-Z]{9}","(?:MG/DL |MMOL/L)"') + _DUMP_LINE_RE = re.compile( + r'P (?P"[A-Z]{3}","[0-9/]{8}","[0-9:]{8} "),' + r'"(?P[C ]) (?P[0-9]{3})(?P[\? ])",' + r'"(?P[NBA])","(?P0[0-9]|1[01])", 00') - def get_readings(self, unit=None): + def get_readings(self): """Iterates over the reading values stored in the glucometer. Args: @@ -235,8 +238,6 @@ class Device(object): if not match: raise exceptions.InvalidResponse(header) - if not unit: - unit = self._parse_glucose_unit(match.group(2)) count = int(match.group(1)) assert count == len(data) @@ -247,12 +248,12 @@ class Device(object): if not match: raise exceptions.InvalidResponse(line) - date = self._parse_datetime(match.group(1)) + line_data = match.groupdict() + + date = self._parse_datetime(line_data['datetime']) # OneTouch2 always returns the data in mg/dL even if the # glucometer is set to mmol/L. We need to convert it to the # requested unit here. - value = common.convert_glucose_unit(int(match.group(2)), - common.UNIT_MGDL, unit) - - yield (date, value) + yield common.Reading(date, int(line_data['value']), + common.UNIT_MGDL) -- cgit v1.2.3